﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS.Resources;
using PMS.Win;
using PMS.Utils;
using PMS.ValidationRules;

namespace PMS.DanhMucChung
{
    public partial class VendorEntry : FormEntryBase
    {
        #region Constants
        private string NEW_String = Messages.GetString(Constants.NEW);
        private const string BINDING_PROPERTY = "EditValue";
        #endregion

        #region Private Members
        protected NhaCungCapBO m_Service;
        #endregion

        #region Properties
        protected Entity.NhaCungCap CurrentVendorEntry
        {
            get
            {
                return base.Entity as Entity.NhaCungCap;
            }
            set
            {
                base.Entity = value;
            }
        }
        #endregion

        #region Services
        public NhaCungCapBO Service
        {
            get
            {
                if (m_Service == null)
                {
                    m_Service = new NhaCungCapBO();
                }
                return m_Service;
            }
        }
        #endregion

        #region Constructors
        public VendorEntry(string functionID)
            : base(functionID)
        {
            InitializeComponent();
            Init();
        }
        #endregion

        #region Init Data and Style
        private void Init()
        {
            InitData();
            InitStyle();
            CanPrint = true;
        }

        private void InitData()
        {
        }

        private void InitStyle()
        {
            bteCode.Tag = "Irgnore";
            txtName.BackColor = REQUIRED_FIELD_COLOR;
            txtAddress.BackColor = REQUIRED_FIELD_COLOR;

            this.bteCode.Properties.MaxLength = 6;
            this.txtName.Properties.MaxLength = 50;
            this.txtAddress.Properties.MaxLength = 80;
            this.txtPhone.Properties.MaxLength = 15;
            this.txtFax.Properties.MaxLength = 15;
            this.txtTaxCode.Properties.MaxLength = 15;
            this.txtEmail.Properties.MaxLength = 30;
            this.txtWeb.Properties.MaxLength = 35;
            this.txtNotes.Properties.MaxLength = 100;
        }
        #endregion

        #region override methods

        protected override void ClearData()
        {
            New();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (this.CurrentVendorEntry == null)
            {
                this.DoNew();
            }
        }

        protected override void BindingToControls(IBaseEntity entity)
        {
            base.BindingToControls(entity);

            this.bteCode.DataBindings.Clear();
            this.txtName.DataBindings.Clear();
            this.txtAddress.DataBindings.Clear();
            this.txtPhone.DataBindings.Clear();
            this.txtFax.DataBindings.Clear();
            this.txtTaxCode.DataBindings.Clear();
            this.txtEmail.DataBindings.Clear();
            this.txtWeb.DataBindings.Clear();
            this.txtNotes.DataBindings.Clear();

            this.bteCode.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.MaNCC.ToString());
            this.txtName.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.TenNCC.ToString());
            this.txtAddress.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.DiaChi.ToString());
            this.txtPhone.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.SoDT.ToString());
            this.txtFax.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.SoFax.ToString());
            this.txtTaxCode.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.MaSoThue.ToString());
            this.txtEmail.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.Email.ToString());
            this.txtWeb.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.Website.ToString());
            this.txtNotes.DataBindings.Add(BINDING_PROPERTY, entity, NhaCungCapColumns.GhiChu.ToString());
        }

        protected override void FocusFirtsControl()
        {
            base.FocusFirtsControl();
            this.txtName.Focus();
        }

        protected override void InitValidationRules()
        {
            base.InitValidationRules();
            Validator validate = new Validator(ValidationProvider);
            validate.AddValidationRule(base.CreateNotEmptyValidationRule());
            SetValidator(validate, txtAddress);
            SetValidator(validate, txtName);

            validate = new Validator(ValidationProvider);
            validate.AddValidationRule(new VendorValidationRules(NEW_String));
            SetValidator(validate, bteCode);

        }

        public override void New()
        {
            this.CurrentVendorEntry = new Entity.NhaCungCap
                {
                    MaNCC = NEW_String
                };
            base.New();
        }

        protected override bool Save()
        {
             try
            {
                if (IsNew)
                {
                    CurrentVendorEntry = Service.CreateNhaCungCap(CurrentVendorEntry);
                }
                else
                {
                    Service.UpdateNhaCungCap(CurrentVendorEntry);
                }
                return true;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
                return false;
            }
        }

        protected override bool Delete()
        {
            try
            {
                Service.DeleteNhaCungCap(CurrentVendorEntry);
                return true;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
                return false;
            }              
        }

        public override void Cancel()
        {
            //your code here
            base.Cancel();
        }

        protected override void Print()
        {
            PMS.ReportViewer.NhaCungCapViewer form = new ReportViewer.NhaCungCapViewer();
            form.WindowState = FormWindowState.Maximized;
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        protected override IBaseEntity GetEntity()
        {
            //need to get the latest record of the current Entity's ID or code then return it
            return base.GetEntity();
        }
        #endregion

        #region Lookups
        #endregion

        #region Form's function
        private void LoadObjectOnForm(string previousCode, string code, IBaseEntity ObjectLoad)
        {
            if (!previousCode.Equals(code, StringComparison.OrdinalIgnoreCase))
            {
                if (IsNew)
                {
                    if (ObjectLoad != null)
                    {
                        if (IsDataChanged)
                        {
                            var result = ShowSaveConfimMessage();
                            if (result == DialogResult.Yes)
                            {
                                CurrentVendorEntry.MaNCC = previousCode;
                                if (!base.ValidateControls() || !Save())
                                {
                                    return;
                                }
                            }
                        }
                        Entity = ObjectLoad;
                        IsNew = false;
                        IsDataChanged = false;
                    }
                    else
                    {
                        CurrentVendorEntry.MaNCC = code;
                        Entity = CurrentVendorEntry;
                        IsNew = true;//important
                        IsDataChanged = true;//important                        
                    }
                }
                else
                {
                    if (!IsDataChanged)
                    {
                        if (ObjectLoad != null)
                        {
                            Entity = ObjectLoad;
                            IsNew = false;
                            IsDataChanged = false;
                        }
                        else
                        {
                            CurrentVendorEntry = new NhaCungCap();
                            CurrentVendorEntry.MaNCC = code;
                            //m_CurrentMaterial.Active = true;
                            Entity = CurrentVendorEntry;
                            IsNew = true;//important
                            IsDataChanged = true;//important   
                        }
                    }
                    else
                    {
                        var result = ShowSaveConfimMessage();
                        if (result == DialogResult.Yes)
                        {
                            CurrentVendorEntry.MaNCC = previousCode;
                            if (!base.ValidateControls() || !Save())
                            {
                                return;
                            }
                        }

                        if (ObjectLoad != null)
                        {
                            Entity = ObjectLoad;
                            IsNew = false;
                            IsDataChanged = false;
                        }
                        else
                        {
                            CurrentVendorEntry = new NhaCungCap();
                            CurrentVendorEntry.MaNCC = code;
                            //m_CurrentMaterial.Active = true;
                            Entity = CurrentVendorEntry; ;
                            IsNew = true;//important
                            IsDataChanged = true;//important 
                        }
                    }
                }
                ClearErrorMessage();
            }
        }
        #endregion

        private void bteCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {
                var form = new VendorLookup();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    IsNew = false;
                    if (form.VendorSelected != null)
                    {
                        LoadObjectOnForm(bteCode.Text.Trim(), form.VendorSelected.MaNCC, form.VendorSelected);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void bteCode_Leave(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(bteCode.Text))
                {
                    New();
                }
                else
                {
                    string previousCode = DataConvertUltils.ToString(CurrentVendorEntry.MaNCC);
                    string code = DataConvertUltils.ToString(bteCode.EditValue);
                    if (!previousCode.Equals(code, StringComparison.OrdinalIgnoreCase))
                    {
                        NhaCungCap temVendor = Service.GetNhaCungCapByID(code);
                        LoadObjectOnForm(previousCode, code, temVendor);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void bteCode_Enter(object sender, EventArgs e)
        {
            if (bteCode.Text.Equals(NEW_String))
            {
                bteCode.Text = string.Empty;
            }
        }
    }
}
